package com.milk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.milk.dto.GoodsSalesDTO;
import com.milk.entity.Orders;
import com.milk.entity.User;
import com.milk.mapper.OrdersMapper;
import com.milk.mapper.UserMapper;
import com.milk.service.ReportService;
import com.milk.vo.OrderReportVO;
import com.milk.vo.SalesTop10ReportVO;
import com.milk.vo.TurnoverReportVO;
import com.milk.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计接口
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        // 计算日期
        List<LocalDate> timeList = new ArrayList();

        timeList.add(begin);
        while (true){
            if (begin.equals(end)) break;
            begin = begin.plusDays(1);
            timeList.add(begin);
        }

        String dateList = StringUtils.join(timeList,",");

        // 存放每天的营业额
        List<Double> list = new ArrayList<>();
        for (LocalDate localDate : timeList) {
            LocalDateTime orderBegin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime orderEnd = LocalDateTime.of(localDate, LocalTime.MAX);

            QueryWrapper<Orders> qw = new QueryWrapper<>();
            qw.ge("order_time",orderBegin);
            qw.le("order_time",orderEnd);
            List<Orders> list1 = ordersMapper.selectList(qw);
            double sum = list1.stream().mapToDouble(Orders::getAmount).sum();

            list.add(sum);
        }
        String turnoverList = StringUtils.join(list, ",");

        return new TurnoverReportVO(dateList,turnoverList);
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    public UserReportVO userStaistics(LocalDate begin, LocalDate end) {
        // 计算日期
        List<LocalDate> timeList = new ArrayList();

        timeList.add(begin);
        while (true){
            if (begin.equals(end)) break;
            begin = begin.plusDays(1);
            timeList.add(begin);
        }

        String dateList = StringUtils.join(timeList,",");


        //存放总用户
        List<Long> totalList = new ArrayList<>();
        //存放新创建的用户
        List<Long> newList = new ArrayList<>();

        for (LocalDate localDate : timeList) {
            LocalDateTime orderBegin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime orderEnd = LocalDateTime.of(localDate, LocalTime.MAX);
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.le("create_time",orderEnd);
            //查询总用户
            Long total = userMapper.selectCount(qw);
            qw.ge("create_time",orderBegin);
            //查询新建用户
            Long newUser = userMapper.selectCount(qw);

            totalList.add(total);
            newList.add(newUser);
        }
        String totalUserList = StringUtils.join(totalList, ",");
        String newUserList = StringUtils.join(newList, ",");

        return new UserReportVO(dateList,totalUserList,newUserList);
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        // 计算日期
        List<LocalDate> timeList = new ArrayList();

        timeList.add(begin);
        while (true){
            if (begin.equals(end)) break;
            begin = begin.plusDays(1);
            timeList.add(begin);
        }

        String dateList = StringUtils.join(timeList,",");

        //存放每一天的订单数
        List<Long> list = new ArrayList<>();
        for (LocalDate localDate : timeList) {
            LocalDateTime orderBegin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime orderEnd = LocalDateTime.of(localDate, LocalTime.MAX);

            QueryWrapper<Orders> qw = new QueryWrapper<>();
            qw.ge("order_time",orderBegin);
            qw.le("order_time",orderEnd);
            Long l = ordersMapper.selectCount(qw);
            list.add(l);
        }

        String orderCountList = StringUtils.join(list, ",");
        return new OrderReportVO(dateList,orderCountList);
    }

    /**
     * 查询销量排名top10
     * @return
     */
    public SalesTop10ReportVO getTop10() {
        List<GoodsSalesDTO> list = ordersMapper.getTop10();

        List<String> names = list.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers = list.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());

        String nameList = StringUtils.join(names, ",");
        String numberList = StringUtils.join(numbers, ",");

        return new SalesTop10ReportVO(nameList,numberList);
    }


}
